package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"gitee.com/rocket049/mycrypto"
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), "%s <input file(输入文件)> <output file(输出文件)>\n如果\"output file(输出文件)\"是减号\"-\"，那么将输出到标准输出(STDOUT),解密程序可以不指定输出文件的名字，它会自动使用原来的文件名。\noptions:\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func main() {
	var mode = flag.String("m", "cfb", "aes encrypto mode: ctr / cfb")
	flag.Parse()
	if len(flag.Args()) != 2 {
		flag.Usage()
		return
	}
	src := flag.Arg(0)
	dst := flag.Arg(1)
	//log.SetFlags(log.LstdFlags | log.Lshortfile)
	//null, _ := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModePerm)
	//log.SetOutput(os.Stderr)

	var pwd string
	fmt.Fprintf(os.Stderr, "Password:")
	_, err := fmt.Scanf("%s", &pwd)
	if err != nil {
		log.Fatalln(err)
	}

	switch *mode {
	case "ctr":
		err = mycrypto.EncryptoToFile(src, dst, pwd)
	case "cfb":
		err = mycrypto.CfbEncryptoToFile(src, dst, pwd)
	default:
		err = mycrypto.EncryptoToFile(src, dst, pwd)
	}

	if err != nil {
		log.Fatal(err)
	}
}
